package com.tianze.tzkf.function.aboutUs;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianze.tzkf.MainActivity;
import com.tianze.tzkf.R;
import com.tianze.tzkf.base.BaseActivity;
import com.tianze.tzkf.base.BaseFragment;
import com.tianze.tzkf.bean.GetVersionBean;
import com.tianze.tzkf.net.RetrofitFactory;
import com.tianze.tzkf.utils.ApkInfoUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Zcyang on 2018/12/27.
 */
public class AboutUsFragment extends BaseFragment {
    private static String TAG = AboutUsFragment.class.toString();

    @BindView(R.id.tv_service_tel)
    TextView tvServiceTel;
    @BindView(R.id.ll_about_us_service_tel)
    LinearLayout llAboutUsServiceTel;
    @BindView(R.id.tv_about_us_allow_update)
    TextView tvAboutUsAllowUpdate;
    @BindView(R.id.tv_about_us_app_version)
    TextView tvAboutUsAppVersion;
    @BindView(R.id.ll_about_us_update)
    LinearLayout llAboutUsUpdate;
    Unbinder unbinder;

    GetVersionBean.DataBean dataBean;

    public static AboutUsFragment newInstance() {

        Bundle args = new Bundle();

        AboutUsFragment fragment = new AboutUsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_about_us;
    }


    @Override
    protected void initTitle() {

    }

    @Override
    protected void initData() {
        final String app_version = ApkInfoUtil.getAppVersionName(AboutUsFragment.this.getActivity());
        tvAboutUsAppVersion.setText(app_version);
    }


    @OnClick({R.id.ll_about_us_service_tel, R.id.ll_about_us_update})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_about_us_service_tel:
                break;
            case R.id.ll_about_us_update:
                GetVersion();
                break;
        }


    }



    public void GetVersion(){
        BaseActivity activity = (BaseActivity) getActivity();
        String token = activity.readToken().getToken();
        String version = ApkInfoUtil.getAppVersionName(activity);



        Map<String, String> request = new HashMap<>();
        request.put("token", token);
        request.put("version", version);

        Observable<GetVersionBean> getSmilePayNumber = RetrofitFactory.getInstance(TAG).getVersion(request);
        getSmilePayNumber.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GetVersionBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(GetVersionBean getVersionBean) {
                        if (getVersionBean.getCode() == 10001) {
                            if ( getVersionBean.getData()!=null && getVersionBean.getData().size()!=0){
                                dataBean = getVersionBean.getData().get(0);
                                //如果有新版本就显示更新弹窗,并传入更新数据
                                Fragment mFragment = getFragmentManager().findFragmentByTag("AboutUsUpdateDialog");
                                if (mFragment == null) {
                                    AboutUsUpdateDialog mAboutUsUpdateDialog = AboutUsUpdateDialog.newInstance(getActivity(), "",dataBean);
                                    mAboutUsUpdateDialog.show(getFragmentManager(), "AboutUsUpdateDialog");
                                }

                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


}
